﻿using System.Collections.Generic;
using Test30.Util;

namespace Test30.Engine
{
    public abstract class Action
    {
        public static implicit operator ActionResult(Action action)
        {
            return new ActionResult(action);
        }
        public Game Game => _game ?? Entity.Dungeon.Game;
        public Dungeon Dungeon => Game.Dungeon;
        public Entity Entity { get; }
        protected Action(NotNull<Entity> entity)
        {
            Entity = entity;
        }

        protected Action(NotNull<Game> game)
        {
            _game = game;
        }
        public void AddAction(NotNull<Action> action)
        {
            _actions.Enqueue(action);
        }

        public void AfterSuccess()
        {
            if (_consumesEnergy && (Entity != null))
            {
                Entity.Energy.Spend();

                // only consume once
                _consumesEnergy = false;
            }
        }
        public void MarkAsEnergyTaking()
        {
            _consumesEnergy = true;
        }
        public ActionResult Process(IList<Effect> effects, Queue<Action> actions)
        {
            _effects = effects;
            _actions = actions;

            ActionResult result = OnProcess();

            _effects = null;
            _actions = null;

            return result;
        }

        public ActionResult Fail()
        {
            return ActionResult.Fail;
        }
        protected abstract ActionResult OnProcess();
        private IList<Effect> _effects;
        private Queue<Action> _actions;
        private readonly Game _game;
        private bool _consumesEnergy;
    }
}
